package gui;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.logging.Level;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;

import entity.Player;

import tools.Input;
import world.LevelManager;
import world.Platform;

public class GameWindow extends JFrame
{
	private final int X = 400;
	private final int Y = 30;
	private final int HEIGHT = 600;
	private final int WIDTH = 800;
	
	private final int BG_LAYER = 1;
	private final int ENTITY_LAYER = 2;
	
	private JLayeredPane gamePane;
	private JLabel background;
	
	private Platform ground;
	private Player player;

	//debug
	private JLabel df;
	
	public GameWindow()
	{
		setup();
	}
	
	private void setup()
	{
		setBounds(X, Y, WIDTH, HEIGHT);
		setLayout(null);
		
		gamePane = new JLayeredPane();
		gamePane.setBounds(0,0,WIDTH, HEIGHT);
		add(gamePane);
		
		background = new JLabel();
		background.setBounds(0, 0, WIDTH, HEIGHT);
		gamePane.add(background);
		gamePane.setLayer(background, BG_LAYER);
		
		player = new Player(this);
		gamePane.add(player);
		gamePane.setLayer(player, ENTITY_LAYER);
		
		for(Platform p : LevelManager.getInstance().getCurrentLevel().getPlatforms()) {
			gamePane.add(p);
			gamePane.setLayer(p, ENTITY_LAYER);
		}
		
		//debug
		df =  new JLabel();
		df.setBounds(10,10,100,20);
		df.setForeground(Color.BLUE);
		gamePane.add(df);
		gamePane.setLayer(df, ENTITY_LAYER);
		
		addKeyListener(new Input());
	}
	
	public void showFPS(float fps)
	{
		df.setText("FPS: " + fps);
	}
	
	public void renderBackground(Camera cam)
	{
		background.setIcon(new ImageIcon(cam.getFrame()));
	}
	
	public void renderPlayer(Camera cam)
	{
		player.render(cam);
	}
	
	public void renderLevel(Camera cam)
	{
		for(Platform p : LevelManager.getInstance().getCurrentLevel().getPlatforms()) {
			p.render(cam);
		}
	}
}

